/**
 * 研发流程状态机 - 权限配置
 * 根据研发流程状态机.md文档实现
 */

const { STATE_CODES } = require('./research_state');
const { STATE_EVENTS } = require('./research_transition');

/**
 * 角色定义
 */
const ROLES = {
  // 管理角色
  ADMIN: 'admin',                     // 管理员
  RESEARCH_MANAGER: 'research_manager', // 研发经理
  DIRECTOR: 'director',               // 董事长
  
  // 研发角色
  HARDWARE_ENGINEER: 'hardware_engineer', // 硬件工程师
  SOFTWARE_ENGINEER: 'software_engineer', // 软件工程师
  TESTER: 'tester',                   // 测试人员
  
  // 业务角色
  BUSINESS_STAFF: 'business_staff',   // 业务人员
  
  // 生产角色
  SAMPLE_TEAM: 'sample_team',         // 样品组
  PURCHASER: 'purchaser',             // 采购人员
  DOCUMENT_CONTROLLER: 'document_controller', // 文控
  WAREHOUSE_KEEPER: 'warehouse_keeper', // 仓库管理员
  RECEPTIONIST: 'receptionist',       // 前台
  SECRETARY: 'secretary',             // 文秘
  PRODUCTION_SUPERVISOR: 'production_supervisor', // 生产主管
  PRODUCTION_ENGINEER: 'production_engineer', // 生产工程师(PE)
  SAMPLE_MANAGER: 'sample_manager'    // 样品管理员
};

/**
 * 角色名称映射
 */
const ROLE_NAMES = {
  [ROLES.ADMIN]: '管理员',
  [ROLES.RESEARCH_MANAGER]: '研发经理',
  [ROLES.DIRECTOR]: '董事长',
  [ROLES.HARDWARE_ENGINEER]: '硬件工程师',
  [ROLES.SOFTWARE_ENGINEER]: '软件工程师',
  [ROLES.TESTER]: '测试人员',
  [ROLES.BUSINESS_STAFF]: '业务人员',
  [ROLES.SAMPLE_TEAM]: '样品组',
  [ROLES.PURCHASER]: '采购人员',
  [ROLES.DOCUMENT_CONTROLLER]: '文控',
  [ROLES.WAREHOUSE_KEEPER]: '仓库管理员',
  [ROLES.RECEPTIONIST]: '前台',
  [ROLES.SECRETARY]: '文秘',
  [ROLES.PRODUCTION_SUPERVISOR]: '生产主管',
  [ROLES.PRODUCTION_ENGINEER]: '生产工程师(PE)',
  [ROLES.SAMPLE_MANAGER]: '样品管理员'
};

/**
 * 操作权限定义
 * 格式: {state}.{subState}: { operations: { event: [roles] } }
 */
const PERMISSIONS = {
  // === 项目审核阶段权限 ===
  'project_review.director_review': {
    view: ['all'],
    transit: ['director'],
    operations: {
      'APPROVE': ['director'],
      'REJECT': ['director']
    }
  },
  'project_review.manager_review': {
    view: ['all'],
    transit: ['research_manager'],
    operations: {
      'APPROVE': ['research_manager'],
      'REJECT': ['research_manager']
    }
  },
  'project_review.assignment': {
    view: ['all'],
    transit: ['research_manager'],
    operations: {
      'SUBMIT': ['research_manager'],
      'APPROVE_DESIGN_CHANGE': ['research_manager']
    }
  },
  
  // === 休眠阶段权限 ===
  'dormant.review': {
    view: ['all'],
    transit: ['research_manager'],
    operations: {
      'APPROVE': ['research_manager'],
      'REJECT': ['research_manager']
    }
  },
  'dormant.archived': {
    view: ['all'],
    transit: ['business'],
    operations: {
      'AWAKEN': ['business']
    }
  },
  
  // === 活动阶段权限 ===
  // 硬件确认
  'active.hardware_confirm': {
    view: ['all'],
    transit: ['hw_engineer'],
    operations: {
      'NO_HARDWARE': ['hw_engineer'],
      'HAS_HARDWARE': ['hw_engineer'],
      'HIBERNATE': ['business'],
      'REQUEST_REDESIGN': ['business']
    }
  },
  
  // 硬件
  'active.hardware': {
    view: ['all'],
    transit: ['hw_engineer', 'sw_engineer'],
    operations: {
      'REQUEST_DEBUG': ['sw_engineer'],
      'HIBERNATE': ['business'],
      'REQUEST_REDESIGN': ['business']
    }
  },
  
  // 调试
  'active.debug': {
    view: ['all'],
    transit: ['sw_engineer'],
    operations: {
      'DEBUG_COMPLETE': ['sw_engineer'],
      'SYNC_DATA': ['sw_engineer'],
      'RECONFIRM_HARDWARE': ['sw_engineer'],
      'HIBERNATE': ['business'],
      'REQUEST_REDESIGN': ['business']
    }
  },
  
  // 资料同步
  'active.data_sync': {
    view: ['all'],
    transit: ['sw_engineer_delegate'],
    operations: {
      'SYNC_COMPLETE': ['sw_engineer_delegate'],
      'HIBERNATE': ['business'],
      'REQUEST_REDESIGN': ['business']
    }
  },
  
  // 测试
  'active.test': {
    view: ['all'],
    transit: ['test_team'],
    operations: {
      'TEST_PASS': ['test_team'],
      'TEST_FAIL': ['test_team'],
      'HIBERNATE': ['business'],
      'REQUEST_REDESIGN': ['business']
    }
  },
  
  // 客户验证
  'active.customer_validation': {
    view: ['all'],
    transit: ['business'],
    operations: {
      'REQUEST_ORDER': ['business'],
      'ISSUE_FOUND': ['business'],
      'HIBERNATE': ['business'],
      'REQUEST_REDESIGN': ['business']
    }
  },
  
  // 订单准备
  'active.order_prepare': {
    view: ['all'],
    transit: ['business', 'production_admin'],
    operations: {
      'CANCEL_ORDER': ['business'],
      'PREPARE_COMPLETE': ['production_admin'],
      'HIBERNATE': ['business'],
      'REQUEST_REDESIGN': ['business']
    }
  },
  
  // 生产
  'active.production': {
    view: ['all'],
    transit: ['business', 'production_manager'],
    operations: {
      'CANCEL_PRODUCTION': ['business'],
      'PRODUCTION_COMPLETE': ['production_manager'],
      'HIBERNATE': ['business'],
      'REQUEST_REDESIGN': ['business']
    }
  },
  
  // 待反馈
  'active.pending_feedback': {
    view: ['all'],
    transit: ['business'],
    operations: {
      'REQUEST_ORDER': ['business'],
      'ISSUE_FOUND': ['business'],
      'HIBERNATE': ['business'],
      'REQUEST_REDESIGN': ['business']
    }
  }
};

/**
 * 资源访问权限
 * 格式: {resourceType}: { view: [roles], download: [roles], upload: [roles] }
 */
const RESOURCE_PERMISSIONS = {
  // 硬件资源
  'schematic': {
    view: ['all'],
    download: [ROLES.HARDWARE_ENGINEER, ROLES.SOFTWARE_ENGINEER, ROLES.RESEARCH_MANAGER, ROLES.DIRECTOR],
    upload: [ROLES.HARDWARE_ENGINEER]
  },
  'pcb': {
    view: ['all'],
    download: [ROLES.HARDWARE_ENGINEER, ROLES.SOFTWARE_ENGINEER, ROLES.RESEARCH_MANAGER, ROLES.DIRECTOR],
    upload: [ROLES.HARDWARE_ENGINEER]
  },
  'gerber': {
    view: [ROLES.HARDWARE_ENGINEER, ROLES.RESEARCH_MANAGER, ROLES.DIRECTOR, ROLES.DOCUMENT_CONTROLLER, ROLES.PURCHASER],
    download: [ROLES.HARDWARE_ENGINEER, ROLES.RESEARCH_MANAGER, ROLES.DIRECTOR, ROLES.DOCUMENT_CONTROLLER, ROLES.PURCHASER],
    upload: [ROLES.HARDWARE_ENGINEER]
  },
  'bom': {
    view: ['all'],
    download: ['all'],
    upload: [ROLES.HARDWARE_ENGINEER]
  },
  
  // 软件资源
  'source_code': {
    view: [ROLES.SOFTWARE_ENGINEER, ROLES.RESEARCH_MANAGER, ROLES.DIRECTOR],
    download: [ROLES.SOFTWARE_ENGINEER, ROLES.RESEARCH_MANAGER, ROLES.DIRECTOR],
    upload: [ROLES.SOFTWARE_ENGINEER]
  },
  'firmware': {
    view: ['all'],
    download: ['all'],
    upload: [ROLES.SOFTWARE_ENGINEER]
  },
  
  // 文档资源
  'test_report': {
    view: ['all'],
    download: ['all'],
    upload: [ROLES.TESTER]
  },
  'user_manual': {
    view: ['all'],
    download: ['all'],
    upload: [ROLES.SOFTWARE_ENGINEER, ROLES.HARDWARE_ENGINEER, ROLES.DOCUMENT_CONTROLLER]
  },
  
  // 流程资源
  'sleep_reason': {
    view: ['all'],
    download: ['all'],
    upload: [ROLES.BUSINESS_STAFF]
  },
  'wakeup_reason': {
    view: ['all'],
    download: ['all'],
    upload: [ROLES.BUSINESS_STAFF]
  }
};

/**
 * 检查角色是否有操作权限
 * @param {String} statePath 状态路径
 * @param {String} eventCode 事件编码
 * @param {String} role 角色
 * @returns {Boolean} 是否有权限
 */
function hasOperationPermission(statePath, eventCode, role) {
  // 管理员具有所有操作权限
  if (role === ROLES.ADMIN) return true;
  
  // 检查是否存在此状态的权限配置
  if (!PERMISSIONS[statePath]) return false;
  
  // 检查此状态下是否存在此事件的权限配置
  if (!PERMISSIONS[statePath].operations[eventCode]) return false;
  
  // 检查角色是否在允许名单中
  return PERMISSIONS[statePath].operations[eventCode].includes(role);
}

/**
 * 检查角色是否有资源访问权限
 * @param {String} resourceType 资源类型
 * @param {String} operation 操作类型 (view/download/upload)
 * @param {String} role 角色
 * @returns {Boolean} 是否有权限
 */
function hasResourcePermission(resourceType, operation, role) {
  // 管理员具有所有资源权限
  if (role === ROLES.ADMIN) return true;
  
  // 检查是否存在此资源类型的权限配置
  if (!RESOURCE_PERMISSIONS[resourceType]) return false;
  
  // 检查此资源类型是否存在此操作的权限配置
  if (!RESOURCE_PERMISSIONS[resourceType][operation]) return false;
  
  // 检查是否允许所有角色
  if (RESOURCE_PERMISSIONS[resourceType][operation].includes('all')) return true;
  
  // 检查角色是否在允许名单中
  return RESOURCE_PERMISSIONS[resourceType][operation].includes(role);
}

/**
 * 导出权限相关功能
 */
module.exports = {
  ROLES,
  ROLE_NAMES,
  PERMISSIONS,
  RESOURCE_PERMISSIONS,
  hasOperationPermission,
  hasResourcePermission
}; 